<?php
/**
 * @version		$Id: helloworld.php 51 2010-11-22 01:33:21Z chdemko $

 * @package		Joomla16.Tutorials

 * @subpackage	Components

 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.

 * @author		Christophe Demko

 * @link		http://joomlacode.org/gf/project/helloworld_1_6/

 * @license		License GNU General Public License version 2 or later

 */



// No direct access to this file

defined('_JEXEC') or die('Restricted access');



// import Joomla modelform library

jimport('joomla.application.component.modeladmin');

class RegionsModelCity extends JModelAdmin

{

	/**

	 * Returns a reference to the a Table object, always creating it.

	 *

	 * @param	type	The table type to instantiate

	 * @param	string	A prefix for the table class name. Optional.

	 * @param	array	Configuration array for model. Optional.

	 * @return	JTable	A database object

	 * @since	1.6

	 */

	public function getTable($type = 'city', $prefix = 'RegionsTable', $config = array()) 

	{

		return JTable::getInstance($type, $prefix, $config);

	}

	/**

	 * Method to get the record form.

	 *

	 * @param	array	$data		Data for the form.

	 * @param	boolean	$loadData	True if the form is to load its own data (default case), false if not.

	 * @return	mixed	A JForm object on success, false on failure

	 * @since	1.6

	 */

	public function getForm($data = array(), $loadData = true) 
	{
		// Get the form.
		$form = $this->loadForm('com_regions.city', 'city', array('control' => 'jform', 'load_data' => $loadData));

		if (empty($form)) 
		{
			return false;
		}
		return $form;
	}

	

	protected function canEditState($record)

	{

		$user = JFactory::getUser();



		// Check against the category.

		if (!empty($record->catid))

		{

			return $user->authorise('core.edit.state', 'com_regions.city.' . (int) $record->id);

		}

		// Default to component settings if category not known.

		else

		{

			return parent::canEditState($record);

		}

	}

	

	/**

	 * Method to get the script that have to be included on the form

	 *

	 * @return string	Script files

	 */

	public function getScript() 

	{

		return 'administrator/components/com_regions/models/forms/scripts.js';

	}

	/**

	 * Method to get the data that should be injected in the form.

	 *

	 * @return	mixed	The data for the form.

	 * @since	1.6

	 */

	protected function loadFormData() 

	{

		// Check the session for previously entered form data.

		$data = JFactory::getApplication()->getUserState('com_regions.edit.city.data', array());

		if (empty($data)) 

		{

			$data = $this->getItem();

		}

		return $data;

	}

}

